using System;
using System.Xml;
using System.IO;

using Icodeon;
using Icodeon.Xhtml;
using Icodeon.Util;

using NUnit.Framework;

namespace Icodeon.Xhtml
{
    [TestFixture]
    public class XhtmlDocumentStrictTest : BaseTest
    {
        XhtmlDocumentStrict xhtmlStrict;
        [TestFixtureSetUp]
        public void Init()
        {
            this.SetUp();

            IOUtils ioUtils = new IOUtils();

            FileInfo scorm2004wrapper = ioUtils.CombineDirectoryNameAndFileName(this.scorm2ccDirectory, "wrapper_2004.html");
            if (!scorm2004wrapper.Exists)
            {
                throw new Exception("Missing file: " + scorm2004wrapper.FullName);
            }
            this.xhtmlStrict = new XhtmlDocumentStrict();
            this.xhtmlStrict.Load(scorm2004wrapper);

        }

        [TestFixtureTearDown]
        public void Dispose()
        {
        }



        [Test]
        public void TestSeralize()
        {
            try
            {
                Assert.IsNotNull(this.xhtmlStrict);
                DirectoryInfo temp = new DirectoryInfo(System.Environment.ExpandEnvironmentVariables("%TEMP%"));
                IOUtils ioUtils = new IOUtils();
                FileInfo xhtmlFile = ioUtils.CombineDirectoryNameAndFileName(temp, "testfile.html");
                this.xhtmlStrict.Serialize(xhtmlFile);


            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    System.Console.WriteLine(e.InnerException.Message);
                }
                System.Console.WriteLine(e.StackTrace);
                Assert.Fail(e.Message);
            }

        }

        [Test]
        public void TestGetTopLevelFrameset()
        {
            try
            {
                Assert.IsNotNull(this.xhtmlStrict);
                IHtml html = this.xhtmlStrict.RootElement;
                Assert.IsNotNull(html);
                IFrameset topLevelFrameSet = html.GetTopLevelFrameset();
                Assert.IsNotNull(topLevelFrameSet);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    System.Console.WriteLine(e.InnerException.Message);
                }
                System.Console.WriteLine(e.StackTrace);
                Assert.Fail(e.Message);
            }

        }

        [Test]
        public void TestFrames()
        {
            try
            {

                IHtml html = this.xhtmlStrict.RootElement;
                IFrameset topLevelFrameSet = html.GetTopLevelFrameset();
                IFrame[] frames = topLevelFrameSet.GetFrames();
                Assert.IsNotNull(frames);
                Assert.AreEqual(1, frames.Length);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    System.Console.WriteLine(e.InnerException.Message);
                }
                System.Console.WriteLine(e.StackTrace);
                Assert.Fail(e.Message);
            }
        }

        [Test]
        public void TestFrameGetSetSrc()
        {
            try
            {

                IHtml html = this.xhtmlStrict.RootElement;
                IFrameset topLevelFrameSet = html.GetTopLevelFrameset();
                IFrame[] frames = topLevelFrameSet.GetFrames();
                IFrame frame = frames[0];
                string src = frame.GetSrc();
                Assert.AreEqual("scorm_2004_sco/sco.html", src);
                string relativePath = "../../file.html";
                frame.SetSrc(relativePath);
                Assert.AreEqual(relativePath, frame.GetSrc());

            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    System.Console.WriteLine(e.InnerException.Message);
                }
                System.Console.WriteLine(e.StackTrace);
                Assert.Fail(e.Message);
            }
        }
    }
}
